home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 14 / CU Amiga Magazine's Super CD-ROM 14 (1997)(EMAP Images)(GB)(Track 1 of 3)[!][issue 1997-09].iso / CUCD / Programming / IEditor / Examples / MultiSelect / MultiSelect.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-17  |  8.6 KB  |  371 lines

  1. /*
  2.     C source code created by Interface Editor
  3.     Copyright © 1994-1996 by Simone Tellini
  4.  
  5.     Generator:  C.generator 37.15 (6.12.96)
  6.  
  7.     Copy registered to :  Simone Tellini
  8.     Serial Number      : #0
  9. */
  10.  
  11. #include <exec/types.h>
  12. #include <exec/nodes.h>
  13. #include <intuition/intuition.h>
  14. #include <intuition/gadgetclass.h>
  15. #include <libraries/gadtools.h>
  16. #include <clib/exec_protos.h>
  17. #include <clib/intuition_protos.h>
  18. #include <clib/gadtools_protos.h>
  19. #include <clib/graphics_protos.h>
  20. #ifdef PRAGMAS
  21. #include <pragmas/exec_pragmas.h>
  22. #include <pragmas/intuition_pragmas.h>
  23. #include <pragmas/graphics_pragmas.h>
  24. #include <pragmas/gadtools_pragmas.h>
  25. #endif
  26. #include <ctype.h>
  27. #include <string.h>
  28.  
  29. #include "MultiSelect.h"
  30.  
  31.  
  32.  
  33. APTR            VisualInfo;
  34. int            YOffset;
  35. UWORD            XOffset;
  36. struct Screen        *Scr = NULL;
  37. struct TextAttr        *Font, Attr;
  38. UWORD            FontX, FontY;
  39. UBYTE            *PubScreenName = NULL;
  40. struct Window        *MainWnd = NULL;
  41. struct Gadget        *MainGList = NULL;
  42. struct IntuiMessage    MainMsg;
  43. struct Gadget        *MainGadgets[2];
  44.  
  45. UBYTE String0[] = "IEditor - MultiSelect Listview Test";
  46. UBYTE String1[] = "_Quit";
  47. UBYTE String2[] = "Test";
  48. UBYTE String3[] = "This";
  49. UBYTE String4[] = "is";
  50. UBYTE String5[] = "a";
  51. UBYTE String6[] = "test";
  52. UBYTE String7[] = "for";
  53. UBYTE String8[] = "MultiSelect";
  54. UBYTE String9[] = "listviews";
  55.  
  56. struct Node ListNodes[] = {
  57.     &ListNodes[1], (struct Node *)&ListList.mlh_Head, 0, 0, String3,
  58.     &ListNodes[2], &ListNodes[0], 0, 0, String4,
  59.     &ListNodes[3], &ListNodes[1], 0, 0, String5,
  60.     &ListNodes[4], &ListNodes[2], 0, 0, String6,
  61.     &ListNodes[5], &ListNodes[3], 0, 0, String7,
  62.     &ListNodes[6], &ListNodes[4], 0, 0, String8,
  63.     (struct Node *)&ListList.mlh_Tail, &ListNodes[5], 0, 0, String9 };
  64.  
  65. struct MinList ListList = {
  66.     (struct MinNode *)&ListNodes[0], (struct MinNode *)NULL, (struct MinNode *)&ListNodes[6] };
  67.  
  68. UWORD MainGTypes[] = {
  69.     BUTTON_KIND,
  70.     LISTVIEW_KIND,
  71.     NULL };
  72.  
  73. struct Hook ListHook = {
  74.     { 0 },
  75.     (HOOKFUNC)ListHookFunc,
  76.     NULL,
  77.     NULL
  78. };
  79.  
  80. struct NewGadget MainNGad[] = {
  81.     293, 2, 78, 15, (UBYTE *)String1, NULL, GD_Quit, NULL, NULL, (APTR)QuitClicked,
  82.     11, 21, 295, 108, (UBYTE *)String2, NULL, GD_List, NG_HIGHLABEL, NULL, (APTR)ListClicked
  83. };
  84.  
  85. ULONG MainGTags[] = {
  86.     (GT_Underscore), '_', (TAG_DONE),
  87.     (GT_Underscore), '_', (GTLV_Labels), (ULONG)&ListList, (GTLV_CallBack), (ULONG)&ListHook, (TAG_DONE)
  88. };
  89.  
  90. struct TagItem MainWTags[] = {
  91.     { WA_Left, 101 },
  92.     { WA_Top, 60 },
  93.     { WA_Width, 398 },
  94.     { WA_Height, 134 },
  95.     { WA_MinWidth, 0 },
  96.     { WA_MaxWidth, -1 },
  97.     { WA_MinHeight, 0 },
  98.     { WA_MaxHeight, -1 },
  99.     { WA_PubScreen, NULL },
  100.     { WA_Title, (ULONG)String0 },
  101.     { WA_Flags, WFLG_SIZEGADGET|WFLG_DRAGBAR|WFLG_DEPTHGADGET|WFLG_CLOSEGADGET|WFLG_SIMPLE_REFRESH|WFLG_NEWLOOKMENUS },
  102.     { WA_IDCMP, BUTTONIDCMP|LISTVIEWIDCMP|IDCMP_REFRESHWINDOW|IDCMP_GADGETUP|IDCMP_CLOSEWINDOW|IDCMP_VANILLAKEY },
  103.     { WA_Gadgets, NULL },
  104.     { TAG_DONE, NULL }
  105. };
  106.  
  107. WORD ScaleX( WORD value )
  108. {
  109.     return(( WORD )((( FontX * value ) + 4 ) / 8 ));
  110. }
  111.  
  112. WORD ScaleY( WORD value )
  113. {
  114.     return(( WORD )((( FontY * value ) + 4 ) / 8 ));
  115. }
  116.  
  117. static void ComputeFont( UWORD width, UWORD height )
  118. {
  119.     Font = &Attr;
  120.     Font->ta_Name = (STRPTR)Scr->RastPort.Font->tf_Message.mn_Node.ln_Name;
  121.     Font->ta_YSize = FontY = Scr->RastPort.Font->tf_YSize;
  122.     FontX = Scr->RastPort.Font->tf_XSize;
  123.  
  124.     XOffset = Scr->WBorLeft;
  125.     YOffset = Scr->RastPort.TxHeight + Scr->WBorTop;
  126.  
  127.     if( width && height )
  128.         if((( ScaleX( width ) + Scr->WBorRight ) > Scr->Width ) ||
  129.             (( ScaleY( height ) + Scr->WBorBottom + YOffset ) > Scr->Height ))
  130.                 {
  131.                     Font->ta_Name = (STRPTR)"topaz.font";
  132.                     FontX = FontY = Font->ta_YSize = 8;
  133.                 }
  134. }
  135.  
  136. int SetupScreen( void )
  137. {
  138.     if(!( Scr = LockPubScreen( PubScreenName )))
  139.         return( 1L );
  140.  
  141.     ComputeFont( 0, 0 );
  142.  
  143.     if(!( VisualInfo = GetVisualInfo( Scr, TAG_DONE )))
  144.         return( 2L );
  145.  
  146.     return( 0L );
  147. }
  148.  
  149. void CloseDownScreen( void )
  150. {
  151.     if( VisualInfo ) {
  152.         FreeVisualInfo( VisualInfo );
  153.         VisualInfo = NULL;
  154.     }
  155.  
  156.     if( Scr ) {
  157.         UnlockPubScreen( NULL, Scr );
  158.         Scr = NULL;
  159.     }
  160.  
  161. }
  162.  
  163. LONG OpenWnd( struct Gadget *GList, struct TagItem WTags[], struct Window **Wnd )
  164. {
  165.     UWORD        tc;
  166.     UWORD        ww, wh, oldww, oldwh;
  167.  
  168.     if( GList ) {
  169.         tc = 0;
  170.         while( WTags[ tc ].ti_Tag != WA_Gadgets ) tc++;
  171.         WTags[ tc ].ti_Data = (ULONG)GList;
  172.     }
  173.  
  174.     ww = ScaleX( WTags[ WT_WIDTH  ].ti_Data ) + XOffset + Scr->WBorRight;
  175.     wh = ScaleY( WTags[ WT_HEIGHT ].ti_Data ) + YOffset + Scr->WBorBottom;
  176.  
  177.     if(( WTags[ WT_LEFT ].ti_Data + ww ) > Scr->Width  )
  178.         WTags[ WT_LEFT ].ti_Data = Scr->Width  - ww;
  179.     if(( WTags[ WT_TOP  ].ti_Data + wh ) > Scr->Height )
  180.         WTags[ WT_TOP  ].ti_Data = Scr->Height - wh;
  181.  
  182.     oldww = WTags[ WT_WIDTH  ].ti_Data;
  183.     oldwh = WTags[ WT_HEIGHT ].ti_Data;
  184.     WTags[ WT_WIDTH  ].ti_Data = ww;
  185.     WTags[ WT_HEIGHT ].ti_Data = wh;
  186.  
  187.     WTags[8].ti_Data = (ULONG)Scr;
  188.  
  189.     *Wnd = OpenWindowTagList( NULL, &WTags[0] );
  190.  
  191.     WTags[ WT_WIDTH  ].ti_Data = oldww;
  192.     WTags[ WT_HEIGHT ].ti_Data = oldwh;
  193.  
  194.     if(!( *Wnd ))
  195.         return( 4L );
  196.  
  197.     GT_RefreshWindow( *Wnd, NULL );
  198.     return( 0L );
  199. }
  200.  
  201. void CloseWnd( struct Window **Wnd, struct Gadget **GList, struct Menu **Mn )
  202. {
  203.     if( Mn ) {
  204.         if( *Wnd )
  205.             ClearMenuStrip( *Wnd );
  206.  
  207.         FreeMenus( *Mn );
  208.         *Mn = NULL;
  209.     }
  210.     if( *Wnd ) {
  211.         CloseWindow( *Wnd );
  212.         *Wnd = NULL;
  213.     }
  214.     if( GList ) {
  215.         FreeGadgets( *GList );
  216.         *GList = NULL;
  217.     }
  218. }
  219.  
  220. struct Gadget *MakeGadgets( struct Gadget **GList, struct Gadget *Gads[],
  221.     struct NewGadget NGad[], UWORD GTypes[], ULONG GTags[], UWORD CNT )
  222. {
  223.     struct Gadget        *g;
  224.     UWORD            lc, tc;
  225.     struct NewGadget    ng;
  226.  
  227.     if(!( g = CreateContext( GList )))
  228.         return( (struct Gadget *)-1 );
  229.  
  230.     for( lc = 0, tc = 0; lc < CNT; lc++ ) {
  231.  
  232.         CopyMem(( char * )&NGad[ lc ], ( char * )&ng, ( long )sizeof( struct NewGadget ));
  233.         ng.ng_VisualInfo = VisualInfo;
  234.         ng.ng_TextAttr = Font;
  235.         ng.ng_LeftEdge = XOffset + ScaleX( ng.ng_LeftEdge );
  236.         ng.ng_TopEdge  = YOffset + ScaleY( ng.ng_TopEdge  );
  237.         ng.ng_Width    = ScaleX( ng.ng_Width  );
  238.         ng.ng_Height   = ScaleY( ng.ng_Height );
  239.         Gads[ lc ] = g = CreateGadgetA((ULONG)GTypes[ lc ], g, &ng, (struct TagItem *)>ags[ tc ] );
  240.  
  241.         while( GTags[ tc ] )
  242.             tc += 2;
  243.         tc++;
  244.  
  245.         if( !g )
  246.             return( (struct Gadget *)-2 );
  247.     }
  248.  
  249.     return( g );
  250. }
  251.  
  252. __geta4 ULONG ListHookFunc( A0( struct Hook *Hook ), A1( struct LVDrawMsg *Msg ), A2( struct Node *Node ))
  253. {
  254.     ULONG            len;
  255.     struct TextExtent    extent;
  256.  
  257.     if( Msg->lvdm_MethodID != LV_DRAW ) {
  258.         return( LVCB_UNKNOWN );
  259.     }
  260.  
  261.     switch( Msg->lvdm_State ) {
  262.         case LVR_NORMAL:
  263.         case LVR_NORMALDISABLED:
  264.         case LVR_SELECTED:
  265.         case LVR_SELECTEDDISABLED:
  266.         len = TextFit( Msg->lvdm_RastPort, Node->ln_Name,
  267.                        strlen( Node->ln_Name ), &extent, NULL, 1,
  268.                        Msg->lvdm_Bounds.MaxX - Msg->lvdm_Bounds.MinX - 3,
  269.                        Msg->lvdm_Bounds.MaxY - Msg->lvdm_Bounds.MinY + 1 );
  270.  
  271.         Move( Msg->lvdm_RastPort, Msg->lvdm_Bounds.MinX + 2,
  272.               Msg->lvdm_Bounds.MinY + Msg->lvdm_RastPort->TxBaseline );
  273.  
  274.         if( Node->ln_Pri & ML_SELECTED ) {
  275.             SetABPenDrMd( Msg->lvdm_RastPort, Msg->lvdm_DrawInfo->dri_Pens[ FILLTEXTPEN ],
  276.                           Msg->lvdm_DrawInfo->dri_Pens[ FILLPEN ], JAM2 );
  277.         } else {
  278.             SetABPenDrMd( Msg->lvdm_RastPort, Msg->lvdm_DrawInfo->dri_Pens[ TEXTPEN ],
  279.                           Msg->lvdm_DrawInfo->dri_Pens[ BACKGROUNDPEN ], JAM2 );
  280.         }
  281.  
  282.         Text( Msg->lvdm_RastPort, Node->ln_Name, len );
  283.  
  284.         SetAPen( Msg->lvdm_RastPort, Msg->lvdm_DrawInfo->dri_Pens[( Node->ln_Pri & ML_SELECTED ) ? FILLPEN : BACKGROUNDPEN ]);
  285.         RectFill( Msg->lvdm_RastPort, Msg->lvdm_RastPort->cp_x, Msg->lvdm_Bounds.MinY,
  286.                   Msg->lvdm_Bounds.MaxX, Msg->lvdm_Bounds.MaxY );
  287.         break;
  288.     }
  289.  
  290.     return( LVCB_OK );
  291. }
  292.  
  293.  
  294. LONG OpenMainWindow( void )
  295. {
  296.     LONG        ret_code = NULL;
  297.     struct Gadget    *g;
  298.  
  299.     ComputeFont( 398, 134 );
  300.  
  301.     g = MakeGadgets( &MainGList, MainGadgets, MainNGad,
  302.         MainGTypes, MainGTags, Main_CNT );
  303.     if( (LONG)g < 0 )
  304.         return( -((LONG)g) );
  305.     ret_code = OpenWnd( MainGList, MainWTags, &MainWnd );
  306.     if( ret_code )
  307.         return( ret_code );
  308.     return( 0L );
  309. }
  310.  
  311. void CloseMainWindow( void )
  312. {
  313.  
  314.     CloseWnd( &MainWnd, &MainGList, NULL );
  315.  
  316. }
  317.  
  318. LONG HandleMainIDCMP( void )
  319. {
  320.     struct IntuiMessage    *m;
  321.     BOOL            (*func)(void);
  322.     BOOL            running = TRUE;
  323.     int            class;
  324.  
  325.     while( m = GT_GetIMsg( MainWnd->UserPort )) {
  326.  
  327.         CopyMem((char *)m, (char *)&MainMsg, (long)sizeof( struct IntuiMessage ));
  328.  
  329.         class = MainMsg.Class;
  330.  
  331.         GT_ReplyIMsg( m );
  332.  
  333.         switch( class ) {
  334.  
  335.             case    IDCMP_VANILLAKEY:
  336.                 running = HandleMainKeys();
  337.                 break;
  338.  
  339.             case    IDCMP_REFRESHWINDOW:
  340.                 GT_BeginRefresh( MainWnd );
  341.                 GT_EndRefresh( MainWnd, TRUE );
  342.                 break;
  343.  
  344.             case    IDCMP_GADGETUP:
  345.                 func = (( struct Gadget * )MainMsg.IAddress )->UserData;
  346.                 running = (*func)();
  347.                 break;
  348.  
  349.             case    IDCMP_CLOSEWINDOW:
  350.                 running = MainCloseWindow();
  351.                 break;
  352.  
  353.         }
  354.     }
  355.     return( running );
  356. }
  357.  
  358. BOOL HandleMainKeys( void )
  359. {
  360.     BOOL running = TRUE;
  361.  
  362.     switch( tolower( MainMsg.Code )) {
  363.  
  364.         case    'q':
  365.             running = QuitKeyPressed();
  366.         break;
  367.  
  368.     }
  369.     return( running );
  370. }
  371.